$(document).ready(function() {
	if ($(".ajaxLoading").length == 0) {
		$("body").append("<div class='ajaxLoading'>Loading...</div>");
	}
	$('.ajaxLoading').hide().ajaxStart(function() {
		$(this).show();
	}).ajaxStop(function() {
		$(this).hide();
	});

	jQuery.ajaxSetup({
		complete : function(jqXHR, textStatus) {
			if (jqXHR && jqXHR.status == 401) {
				if (!this.error)
					alert("The session is time out,please login again! ");
				location.reload();
				jqXHR.abort();
			}
			if (jqXHR && jqXHR.status == 403) {
				if (!this.error)
					alert("Sorry,you have not permission to do this operation!");

				jqXHR.abort();
			}
		}
	});

	// add new method to jquery validator plugin
	if (jQuery.validator) {
		jQuery.validator.addMethod("isDate", function(value, element) {
			if (!value) {
				return true;
			}
			var ereg = /^(\d{1,2})(-|\/)(\d{1,2})(-|\/)(\d{1,4})$/;
			var r = value.match(ereg);
			if (r == null) {
				return false;
			}
			var d = new Date(r[5], r[1] - 1, r[3]);
			var result = (d.getFullYear() == r[5] && (d.getMonth() + 1) == r[1] && d.getDate() == r[3]);
			return this.optional(element) || (result);
		}, "date format error!");

		jQuery.validator.addMethod("isDatetime", function(value, element) {
			if (!value) {
				return true;
			}
			var arr = value.split(" ");
			if (!arr || arr.length != 2) {
				return false;
			}
			var ereg = /^(\d{1,2})(-|\/)(\d{1,2})(-|\/)(\d{1,4})$/;
			var r = arr[0].match(ereg);
			if (r == null) {
				return false;
			}
			var d = new Date(r[5], r[1] - 1, r[3]);
			arr = arr[1].split(":");
			if (!arr || arr.length != 3) {
				return false;
			}
			if (isNaN(arr[0]) || isNaN(arr[1]) || isNaN(arr[2])) {
				return false;
			}

			var hour = parseInt(arr[0]);
			var minute = parseInt(arr[1]);
			var second = parseInt(arr[2]);
			if (hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
				return false;
			}
			var result = (d.getFullYear() == r[5] && (d.getMonth() + 1) == r[1] && d.getDate() == r[3]);
			return this.optional(element) || (result);
		}, "date time format error!");
	}
});

function toAoDataMap(aoData) {
	var map = [];
	$.each(aoData, function(index, obj) {
		map[obj.name] = obj.value;
	});
	return map;
}

function getDateTablePageInfo(oSettings) {
	var pageinfo = {
		"iStart" : oSettings._iDisplayStart,
		"iEnd" : oSettings.fnDisplayEnd(),
		"iLength" : oSettings._iDisplayLength,
		"iTotal" : oSettings.fnRecordsTotal(),
		"iFilteredTotal" : oSettings.fnRecordsDisplay(),
		"iPage" : Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength),
		"iTotalPages" : Math.ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)
	};
	if (oSettings._iDisplayStart > 0 && oSettings._iRecordsTotal % oSettings._iDisplayLength == 1&&((pageinfo.iPage+1)==Math.ceil(oSettings._iRecordsTotal/ oSettings._iDisplayLength))) {
		pageinfo.iPage = pageinfo.iPage - 1;
	}
	return pageinfo;
}

function getDateTablePageInfoAfterBatchDelete(oSettings,batchSize) {
	var pageinfo = {
		"iStart" : oSettings._iDisplayStart,
		"iEnd" : oSettings.fnDisplayEnd(),
		"iLength" : oSettings._iDisplayLength,
		"iTotal" : oSettings.fnRecordsTotal(),
		"iFilteredTotal" : oSettings.fnRecordsDisplay(),
		"iPage" : Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength),
		"iTotalPages" : Math.ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)
	};
	if (oSettings._iDisplayStart > 0 && oSettings._iRecordsTotal % oSettings._iDisplayLength == batchSize&&((pageinfo.iPage+1)==Math.ceil(oSettings._iRecordsTotal/ oSettings._iDisplayLength))) {
		pageinfo.iPage = pageinfo.iPage - 1;
	}
	if(oSettings._iDisplayStart > 0 && oSettings._iDisplayLength == batchSize&&((pageinfo.iPage+1)==Math.ceil(oSettings._iRecordsTotal/ oSettings._iDisplayLength))){
		pageinfo.iPage = pageinfo.iPage - 1;
	}
	return pageinfo;
}
/**
 * judge a var is a positive integer number.
 * @param value
 */
function isPositiveIntegerNumber(value){
	return /^[0-9]*[1-9][0-9]*$/.test(value);
}
/**
 * judge is a nonnegative number.
 * @param value
 * @returns
 */
function isNonnegativenumber(value){
	return /^\d+$/.test(value);
}
